package com.kzj.laimeimei.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.kzj.laimeimei.fragment.Fragment1;
import com.kzj.laimeimei.fragment.Fragment2;
import com.kzj.laimeimei.fragment.Fragment3;
import com.kzj.laimeimei.fragment.Fragment4;
import com.kzj.laimeimei.global.Constant;
import com.kzj.laimeimei.viewPagerTransforms.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xyg on 2015/9/30.
 */
public class MainActivity extends GBaseActivity {
    private ViewPager viewpage;
    private TabFragmentPagerAdapter tabFragmentPagerAdapter;
    public Map<String, Fragment> fragmentsMap = new HashMap<String, Fragment>();
    private FragmentManager manager;
    Fragment fragment1;
    Fragment fragment2;
    Fragment fragment3;
    Fragment fragment4;
    private static final ArrayList<TransformerItem> TRANSFORM_CLASSES;
    static {
        TRANSFORM_CLASSES = new ArrayList<>();
        TRANSFORM_CLASSES.add(new TransformerItem(DefaultTransformer.class));
        TRANSFORM_CLASSES.add(new TransformerItem(AccordionTransformer.class));
        TRANSFORM_CLASSES.add(new TransformerItem(BackgroundToForegroundTransformer.class));
        TRANSFORM_CLASSES.add(new TransformerItem(DepthPageTransformer.class));
        TRANSFORM_CLASSES.add(new TransformerItem(ForegroundToBackgroundTransformer.class));
        TRANSFORM_CLASSES.add(new TransformerItem(CubeInTransformer.class));
        TRANSFORM_CLASSES.add(new TransformerItem(CubeOutTransformer.class));
        TRANSFORM_CLASSES.add(new TransformerItem(FlipHorizontalTransformer.class));
        TRANSFORM_CLASSES.add(new TransformerItem(FlipVerticalTransformer.class));
        TRANSFORM_CLASSES.add(new TransformerItem(RotateDownTransformer.class));
        TRANSFORM_CLASSES.add(new TransformerItem(RotateUpTransformer.class));
        TRANSFORM_CLASSES.add(new TransformerItem(StackTransformer.class));
        TRANSFORM_CLASSES.add(new TransformerItem(TabletTransformer.class));
        TRANSFORM_CLASSES.add(new TransformerItem(ZoomInTransformer.class));
        TRANSFORM_CLASSES.add(new TransformerItem(ZoomOutTranformer.class));
        TRANSFORM_CLASSES.add(new TransformerItem(ZoomOutSlideTransformer.class));
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findViewByIds();
        initDatas();
    }

    @Override
    protected void findViewByIds() {
        viewpage=null;
    }

    @Override
    protected void initDatas() {
        manager = getSupportFragmentManager();
        initFragments();
        tabFragmentPagerAdapter=new TabFragmentPagerAdapter(manager);
        viewpage.setAdapter(tabFragmentPagerAdapter);
        try {
            viewpage.setPageTransformer(true,TRANSFORM_CLASSES.get(5).clazz.newInstance());
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void fillArrayInListView() {

    }

    private void initFragments() {
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        fragment4 = new Fragment4();



        fragmentsMap.put("1", fragment1);
        fragmentsMap.put("2", fragment2);
        fragmentsMap.put("3", fragment3);
        fragmentsMap.put("4", fragment4);
    }

    class TabFragmentPagerAdapter extends FragmentStatePagerAdapter {


        public TabFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment ft = null;
            switch (position) {
                case 0:

                    ft = fragmentsMap.get("1");
                    break;
                case 1:
                    ft = fragmentsMap.get("2");
                    break;
                case 2:
                    ft = fragmentsMap.get("3");
                    break;
                case 3:
                    ft = fragmentsMap.get("4");
                    break;
                default:
                    break;
            }
            return ft;
        }

        @Override
        public int getCount() {
            return fragmentsMap.size();
        }


    }

    private static final class TransformerItem {

        final String title;
        final Class<? extends ViewPager.PageTransformer> clazz;

        public TransformerItem(Class<? extends ViewPager.PageTransformer> clazz) {
            this.clazz = clazz;
            title = clazz.getSimpleName();
        }

        @Override
        public String toString() {
            return title;
        }

    }

}